package com.wty.love.diary;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wty.love.MainActivity;
import com.wty.love.R;
import com.wty.love.database.entity.Diary;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddDiaryActivity extends AppCompatActivity {
    private View back;
    private View save;

    private EditText title;
    private EditText weather;
    private EditText content;
    private EditText note;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity.setStatusBarColor(this, Color.rgb(250,250,250));
        setContentView(R.layout.activity_add_diary);
        initViews();
        initClick();
    }

    private void initViews(){
        back = findViewById(R.id.back);
        save = findViewById(R.id.save);
        title = findViewById(R.id.title);
        weather = findViewById(R.id.weather);
        content = findViewById(R.id.content);
        note = findViewById(R.id.note);
    }

    private void initClick(){
        back.setOnClickListener(v->{
            onBackPressed();
        });
        save.setOnClickListener(v->{
            LiveEventBus.get(MainActivity.SETDELETEPOSITION).post(0);
            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            String createTime = sdf.format(new Date());
            MainActivity.diaryDao.insertDiary(new Diary(createTime,weather.getText().toString(),title.getText().toString(),content.getText().toString(),note.getText().toString(),"0"));
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            finish();
        });
    }
}